package com.itheima.controller;

import cn.hutool.core.util.StrUtil;
import com.itheima.manager.UserManager;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserManager userManager;

    // 用户列表
    @GetMapping("/manage/users")
    public ResponseEntity findUserInfoByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return userManager.findUserInfoByPage(pageNum, pageSize);
    }

    // 用户详情
    @GetMapping("/manage/users/{userId}")
    public ResponseEntity findById(@PathVariable Long userId) {
        return userManager.findById(userId);
    }

    //动态列表
    @GetMapping("/manage/messages")
    public ResponseEntity findMovementByConditionAndPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "uid", required = false) Long userId,
            @RequestParam(required = false) String state) {

        Integer status = null;
        if (StrUtil.isNotBlank(state)) {
            if (!"''".equals(state)) {
                status = Integer.parseInt(state);
            }
        }

        return userManager.findMovementByConditionAndPage(userId, status, pageNum, pageSize);
    }

    // 单条动态详情
    @GetMapping("/manage/messages/{publishId}")
    public ResponseEntity findMovementById(@PathVariable ObjectId publishId) {
        return userManager.findMovementById(publishId);
    }

    // 动态评论列表
    @GetMapping("/manage/messages/comments")
    public ResponseEntity findMovementComemntByPage(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10", required = false) Integer pageSize,
            @RequestParam(value = "messageID") String publishId) {
        return userManager.findMovementComemntByPage(pageNum, pageSize, new ObjectId(publishId));
    }
    // 动态通过
    @PostMapping("/manage/messages/pass")
    public ResponseEntity MovementPass(@RequestBody String[] ids) {

        return userManager.MovementPass(ids);
    }
    // 动态拒绝
    @PostMapping("/manage/messages/reject")
    public ResponseEntity MovementReject(@RequestBody String[] ids) {

        return userManager.MovementReject(ids);
    }
}